// ===============================================
// smart_refresher_by_grid
// 拓展下拉刷新的组件 封装他的一意义是在于
// 采用统一 的下俩顶部  或者上拉的底部UI
// 采用分帧技术
// 还可以拓展 网络 不通的时候 有那个 网络提示
// 自带动态高度 无需再过多 设置  动态高度实现了 分帧技术 所以次页面移除
// Create by Will on 15/9/2023 20:22
// Copyright Will .All rights reserved.
// ===============================================

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keframe/keframe.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'dynamic_height_grid_view.dart';
import 'page_status.dart';
import 'refresh_indicator.dart';

class SmartRefresherByGrid extends StatelessWidget {
  const SmartRefresherByGrid({
    super.key,
    required this.controller,
    required this.child,
    required this.onRefresh,
    this.itmList = 0,
    this.onLoading,
    this.header,
    this.refreshHeader,
    this.footer,
    this.enablePullDown = true,
    this.enablePullUp = true,
    this.placeHolder,
    this.crossAxisCount,
    this.crossAxisSpacing = 10,
    this.mainAxisSpacing = 10,
    this.physics,
    this.gridController,
    this.shrinkWrap = false,
    this.padding,
    //如果不是web 采用分帧
    this.useFraming = !kIsWeb,
    this.cacheExtent = 500,
    this.refreshFooterColor,
    this.scrollDirection = Axis.vertical,
  });

  //刷新控件
  final RefreshController controller;

  //下拉刷新的方法
  final VoidCallback onRefresh;

  //上拉加载的方法
  final VoidCallback? onLoading;

  //里面放的组件
  final NullableIndexedWidgetBuilder child;

  //头部 一般不需要改
  final Widget? header;

  //自定义 刷新头部
  final Widget? refreshHeader;

  //脚步 一般不需要改
  final Widget? footer;

  //能否上拉
  final bool enablePullUp;

  //能否下来
  final bool enablePullDown;

  //返回的 个数
  final int itmList;

  //这个就是放占位图片的
  final Widget? placeHolder;

  //每一行放多少个
  final int? crossAxisCount;

  //纵向间距
  final double crossAxisSpacing;

  //横向间距
  final double mainAxisSpacing;

  //滚动方向
  final ScrollPhysics? physics;

  //滚动控件
  final ScrollController? gridController;

  //是否支持滚动
  final bool shrinkWrap;

  // 间距的问题
  final EdgeInsetsGeometry? padding;

  //是否需要 分帧技术
  final bool useFraming;

  //列表预加载的高度
  final double cacheExtent;

  final Color? refreshFooterColor;

  //滚动分享
  final Axis? scrollDirection;

  @override
  Widget build(BuildContext context) {
    final body = SmartRefresher(
      enablePullUp: enablePullUp,
      enablePullDown: enablePullDown,
      // header: refreshHeader ?? const RefreshHeader(),
      header: refreshHeader ?? const MaterialClassicHeader(),
      footer: footer ?? RefreshFooter(footerTextColor: refreshFooterColor),
      controller: controller,
      // onRefresh: () {
      //   //下拉刷新的时候 先重置下 下面的没数据提醒
      //   controller.loadComplete();
      //   onRefresh();
      // },
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: _buildScrollView(context: context),
    );
    return useFraming ? SizeCacheWidget(child: body) : body;
  }

  Widget _buildScrollView({required BuildContext context}) {
    if (itmList == 0) {
      if (header != null) {
        return Column(
          children: <Widget>[
            Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                child: header ?? Container()),
            Expanded(child: PageStatus.noData())
          ],
        );
      }
      return PageStatus.noData();
    }

    return DynamicHeightGrid.dynamicHeightGridView(
      scrollDirection: scrollDirection,
      cacheExtent: cacheExtent,
      useFraming: useFraming,
      padding: padding,
      header: header,
      controller: gridController,
      shrinkWrap: shrinkWrap,
      physics: physics,
      itemCount: itmList,
      crossAxisCount: crossAxisCount ?? 1,
      crossAxisSpacing: crossAxisSpacing,
      mainAxisSpacing: mainAxisSpacing,
      // builder: (BuildContext c, int i) => child(c, i)!,
      builder: (c, i) => useFraming
          ? FrameSeparateWidget(
              index: i,
              // placeHolder: placeHolder ??
              //     Container(
              //       color: Theme.of(context).scaffoldBackgroundColor,
              //       height: 68,
              //     ),
              child: child(c, i)!,
            )
          : child(c, i)!,
    );
  }
}
